<script setup lang="ts">
import { ContentWrapNoHeaderLine } from '@/components/ContentWrap'
import { Table } from '@/components/Table'
import { reactive, ref, watch } from 'vue'
import { useTable } from "@/hooks/web/useTable";
import { IPData } from "@/api/auth/ip/types";
import { getIpListApi, delIpApi } from "@/api/auth/ip/index";
import { Pagination, TableColumn } from "@/types/table";

const columns = reactive<TableColumn[]>([
  {
    field: 'id',
    label: 'ID',
    search: false,
    visible: true,
    className: ''
  }, {
    field: 'createTime',
    label: '创建时间',
    search: false,
    visible: true,
    className: ''
  }, {
    field: 'name',
    label: 'IP设备组名称',
    search: true,
    visible: true,
    className: ''
  }, {
    field: 'type',
    label: '可管理设备类型',
    visible: true,
    search: true,
    className: ''
  }, {
    field: 'idArr',
    label: '可管理IP',
    visible: true,
    search: true,
    className: ''
  }
])

const { register, tableObject, methods } = useTable<IPData>({
  getListApi: getIpListApi,
  delListApi: delIpApi,
  response: {
    list: 'list',
    total: 'total'
  }
})
const paginationObj = ref<Pagination>()
watch(() => tableObject.total,
  (val) => {
    if (val > 0) {
      paginationObj.value = {
        total: tableObject.total
      }
    } else {
      paginationObj.value = undefined
    }
  })
const { getList } = methods
getList()

const selectedCount = ref(0)
const multipleSelection = ref<IPData[]>([])
const handleSelectionChange = (val: IPData[]) => {
  multipleSelection.value = val
  selectedCount.value = val?.length || 0
}

const selectedRows = async () => {
  const { getSelections } = methods
  const selections = await getSelections()
  return selections;
}

defineExpose({
  getSelected: selectedRows
})
</script>

<template>
  <ContentWrapNoHeaderLine title="">
    <div class="rounded bg-gray-100 w-full flex justify-between px-1.5 py-2 mb-3.5">
      <div>已批量选择<span class="mx-1 text-[var(--el-color-primary)]">{{ selectedCount }}</span>条事项</div>
    </div>
    <div class="">
      <Table border v-model:pageSize="tableObject.page_size" v-model:currentPage="tableObject.page_index"
        :columns="columns" :data="tableObject.tableList" :loading="tableObject.loading" :pagination="paginationObj"
        @register="register" @select="handleSelectionChange" @select-all="handleSelectionChange" />

    </div>
  </ContentWrapNoHeaderLine>
</template>
